<?php
namespace app\api\model;

class Base extends \think\Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '';
    
    // 设置当前模型的数据库连接
    protected $connection = [];
    
    private $fields = [
        'gs_company' =>'company_id,fullname,zch,tym,province,othernames,companyattr,check_date,sse_industry,sse_lable',
        'sanban_company_info' =>'legalperson,loginaddress,djjg,gslx,company_homepage,email,finance,phone,transferMode,layer,postcode,industry,clrq,hzrq,jyzt,business,yewu'
        
    ];

    /**
     * 查询单条记录
     * @param  string $table 表名
     * @param  array  $where 条件
     * @return [type]        array
     */
    public function item($table ='',$where=array(),$field=array()){
        $fields = $this->fields;
        $field = empty($field) && isset($fields[$table]) ? $fields[$table] : $field;
        if(!$table){
            return array();
        }
        return $this->table($table)->field($field)->where($where)->find();
    }
    /**
     * 查询多条记录
     * @param  string $table  [description]
     * @param  array  $where  [description]
     * @param  string $limits [description]
     * @param  string $order  [description]
     * @return [type]         [description]
     */
    public function items($table ='',$where=array(),$limits='100',$order=array('id',-1),$field=array()){
        $fields = $this->fields;
        $field = empty($field) && isset($fields[$table]) ? $fields[$table] : $field;
        if(!$table){
            return array();
        }
        
        return $this->table($table)->field($field)->where($where)->order($order[0],$order[1])->limit($limits)->select();
    }

    /**
     * 查询记录数
     * @param  string $table 表名
     * @param  array  $where 条件
     * @return [type]        array
     */
    public function itemNum($table ='',$where=array()){
        if(!$table){
            return 0;
        }

        return $this->table($table)->where($where)->count();
    }

    /**
     * 更新
     * @param  string $table 表名
     * @param  array  $where 条件
     * @return [type]        array
     */
    public function itemUpdate($table ='',$where=array(),$data = array()){
        if(!$table || empty($where) || empty($data)){
            return false;
        }
        if($table =='sanban_company_info' || $table =='gs_company'){
            if(!$where['company_id']){
                return false;
            }
        }else{
            if(!$where['id']){
                return false;
            }
        }
        //var_dump($where);exit;
        return $this->table($table)->where($where)->update($data);
    }

    /**
     * 新增
     * @param  string $table 表名
     * @param  array  $where 条件
     * @return [type]        array
     */
    public function itemInsert($table ='',$data = array(),$lastid = null){
        if(!$table || empty($data)){
            return false;
        }
        //是否返回插入的id
        if($lastid){
            return $this->table($table)->insert($data,null,true);
        }else{
            return $this->table($table)->insert($data);
        }
        
    }

    public function lastsql($table =''){
        return $this->table($table)->getlastsql();
    }
}
